package com.example.spring_async.service;

import org.springframework.aop.ThrowsAdvice;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Random;
import java.util.concurrent.CompletableFuture;

/**
 * @Author: banban
 * @Desctription: TODO
 * @Date: Created in 2024/1/20 22:26
 * @Version: 1.0
 */
@Service
public class AsyncService {

    /**
     * 无返回值
     * @param email
     * @param message
     */
    @Async("mainThreadPool")
    public void sendMail(String email,String message){
        try {
            Thread.sleep(new Random().nextInt(3000));
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("发送成功，email:" + email);
    }

    /**
     * 有返回值
     * @param email
     * @param message
     * @return
     */
    @Async("mainThreadPool")
    public CompletableFuture<String> blockSendMail(String email,String message){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("发送成功，email:" + email);
        return CompletableFuture.completedFuture("发送成功，email:" + email);
    }

}
